<template>
  <transition name="fade" @after-leave="afterLeave">
    <div class="xtx-message" :style="style[type]" v-if="show">
      <!-- 上面绑定的是样式 -->
      <!-- 不同提示图标会变 -->
      <span class="text">{{ text }}</span>
    </div>
  </transition>
</template>
<script>
export default {
  name: 'XtxMessage',
  data () {
    return {
      show: false,
      text: '测试文本内容',
      type: 'success',
      duration: 3000,
      style: {
        warn: {
          icon: 'icon-warning',
          color: '#E6A23C',
          backgroundColor: 'rgb(253, 246, 236)',
          borderColor: 'rgb(250, 236, 216)'
        },
        error: {
          icon: 'icon-shanchu',
          color: '#F56C6C',
          backgroundColor: 'rgb(254, 240, 240)',
          borderColor: 'rgb(253, 226, 226)'
        },
        success: {
          icon: 'icon-queren2',
          color: '#67C23A',
          backgroundColor: 'rgb(240, 249, 235)',
          borderColor: 'rgb(225, 243, 216)'
        }
      }
    }
  },
  mounted () {
    this.show = true
    setTimeout(() => {
      this.show = false
    }, this.duration)
  },
  methods: {
    afterLeave () {
      document.body.removeChild(this.$el)
    }
  }
}
</script>
<style scoped lang="scss">
.xtx-message {
  width: 300px;
  height: 50px;
  position: fixed;
  z-index: 9999;
  left: 50%;
  margin-left: -150px;
  top: 25px;
  line-height: 50px;
  padding: 0 25px;
  border: 1px solid #e4e4e4;
  background: #f5f5f5;
  color: #999;
  border-radius: 4px;
  i {
    margin-right: 4px;
    vertical-align: middle;
  }
  .text {
    vertical-align: middle;
  }
}
.fade-enter-active,
.fade-leave-active {
  transition: all 0.5s;
}
.fade-enter {
  transform: translateY(-150px);
}
.fade-leave-to {
  transform: translateY(-150px);
}
</style>
